#include "soc_i2c.h"


/*----I2C主控模式命令-------------------------------------------------------*/
#define I2C_MASTER_START_SEND				(I2C_I2CMCR_START_Msk | I2C_I2CMCR_RUN_Msk)		/*起始位+从机地址+写+1Byte数据*/
#define I2C_MASTER_SEND						(I2C_I2CMCR_RUN_Msk)							/*发送1Byte数据*/
#define I2C_MASTER_STOP						(I2C_I2CMCR_STOP_Msk)							/*发送停止位*/
#define I2C_MASTER_START_RECEIVE_NACK		(I2C_I2CMCR_START_Msk | I2C_I2CMCR_RUN_Msk)		/*起始位+从机地址+读+1Byte时钟+NACK*/
#define I2C_MASTER_START_RECEIVE_ACK		(I2C_I2CMCR_START_Msk | I2C_I2CMCR_RUN_Msk | I2C_I2CMCR_ACK_Msk) /*起始位+从机地址+读+1Byte时钟+ACK*/
#define I2C_MASTER_RECEIVE_NACK				(I2C_I2CMCR_RUN_Msk)							/*发送读1Byte时钟+NACK*/
#define I2C_MASTER_RECEIVE_ACK				(I2C_I2CMCR_RUN_Msk | I2C_I2CMCR_ACK_Msk)		/*发送读1Byte时钟+ACK*/

/*****************************************************************************
 ** \brief	I2C_SendMasterCmd
 **			发送主控命令
 ** \param [in] none
 ** \return  none
 ** \note	 
*****************************************************************************/
void  I2C_SendMasterCmd(uint8_t Cmd)
{
	I2CMCR = Cmd;
}

/*****************************************************************************
 ** \brief	I2C_MasterWriteAddr
 **			主控写从机地址
 ** \param [in] SlaveAddr ：从机地址+读写位R/W
 ** \return  none
 ** \note	 读写位在从机地址的第0位，即7位地址+R/W  
*****************************************************************************/
void  I2C_MasterWriteAddr(uint8_t SlaveAddr)
{
	I2CMSA   = SlaveAddr;
}

/*****************************************************************************
 ** \brief	I2C_MasterWriteBuffer
 **			主控写缓存
 ** \param [in] data
 ** \return  none
 ** \note	 
*****************************************************************************/
void  I2C_MasterWriteBuffer(uint8_t Data)
{
	I2CMBUF = Data;
}

/*****************************************************************************
 ** \brief	I2C_MasterReadBuffer
 **			主控读缓存
 ** \param [in] none
 ** \return  data
 ** \note	 
*****************************************************************************/
uint8_t I2C_MasterReadBuffer(void)
{
	return I2CMBUF;
}

void  I2C_SendMasterCmdAndDataWait(uint8_t cmd, uint8_t b)
{
	I2C_MasterWriteBuffer(b);				/*低位ROM 地址*/
	I2C_SendMasterCmd(cmd);				/*发送Buffer*/
	while(!(I2C_GetMasterIntFlag()));
	I2C_ClearMasterIntFlag();	
}

void  I2C_SendMasterCmdWait(uint8_t cmd)
{
	I2C_SendMasterCmd(cmd);				/*发送Buffer*/
	while(!(I2C_GetMasterIntFlag()));
	I2C_ClearMasterIntFlag();	
}

void SOC_I2CInit(SOC_I2CNo_t no)
{
    /*
    (1)开启I2C主控模式
    */
    I2C_EnableMasterMode();
    /*
    (2)设置I2C通讯时钟
    */	 
    I2C_ConfigCLK(10);		/*设置时钟100K*/
}

void SOC_I2C_ConfigMode(SOC_I2CNo_t no, SOC_I2CMode_t mode)
{
    switch (mode)
    {
    case S_I2C_Master:
        I2C_EnableMasterMode();
        break;
    case S_I2C_Slave:
        I2C_DisableMasterMode();
        break;
    default:
        break;
    }
}

void SOC_I2CDevInit(SOC_I2CDev_t *i2c, SOC_I2CNo_t no, uint8_t wAddr, uint8_t rAddr, uint8_t regByte)
{
    i2c->no = no;
    i2c->slaveWAddr = wAddr;
    i2c->slaveRAddr = rAddr;
    i2c->regAddrByte = regByte;
}

uint8_t SOC_I2CReadByte(SOC_I2CDev_t *i2c, uint16_t regAddr)
{
	uint8_t buffer;
	
	I2C_MasterWriteAddr(i2c->slaveWAddr);			/*写从机地址+写*/
	if(i2c->regAddrByte == 2)
	{
		I2C_SendMasterCmdAndDataWait(I2C_MASTER_START_SEND, (regAddr >> 8) & 0xff);
		I2C_SendMasterCmdAndDataWait(I2C_MASTER_SEND, (regAddr) & 0xff);
	}
	else 
	{
		I2C_SendMasterCmdAndDataWait(I2C_MASTER_START_SEND, regAddr & 0xff);
	}
	
	I2C_MasterWriteAddr(i2c->slaveRAddr);					/*写从机地址+读*/	
	I2C_SendMasterCmdWait(I2C_MASTER_START_RECEIVE_NACK);	
	
	buffer=I2C_MasterReadBuffer();						/*读取数据*/
	
	I2C_SendMasterCmd(I2C_MASTER_STOP);					/*发送停止位*/
	return buffer;
}
